package mo.leetcode_hot;

import java.util.*;

public class L49 {

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> convertKey2values = new HashMap<>();
        int[] bits = new int[26];
        StringBuilder builder = new StringBuilder();
        for (String str : strs) {
            Arrays.fill(bits, 0);
            builder.setLength(0);
            for (char c : str.toCharArray()) {
                bits[c-'a']++;
            }
            for (int i = 0; i < 26; i++) {
                if (bits[i] != 0) {
                    builder.append((char) ('a' + i));
                    builder.append(bits[i]);
                }
            }
            String convertString = builder.toString();
            if (convertKey2values.containsKey(convertString)) {
                convertKey2values.get(convertString).add(str);
            } else {
                List<String> values = new ArrayList<>();
                values.add(str);
                convertKey2values.put(convertString, values);
            }
        }
        return new ArrayList<>(convertKey2values.values());
    }

}
